package utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {
    static SqlSessionFactory sqlSessionFactory=null;
    public static void buildSqlSessionFactory(){
        String configFilePath="MyBatisConfig.xml";
        if (sqlSessionFactory!=null)return;
        try {
            InputStream inputStream= Resources.getResourceAsStream(configFilePath);
            sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static SqlSession openSqlSession(){
        if (sqlSessionFactory==null)buildSqlSessionFactory();
        return sqlSessionFactory.openSession();
    }
}
